import { Style, Circle, Stroke, Fill, Text, Icon } from "ol/style";

/**
 * 根据水质类别获取对应的颜色
 * @param {string|number} qualityLevel - 水质类别 (I, II, III, IV, V, 劣V)
 * @returns {string} 颜色值
 */
export function getWaterQualityColor(qualityLevel) {
  const colorMap = {
    Ⅰ类: "rgb(204, 255, 255)", // 优 #ccffff
    Ⅱ类: "rgb(0, 204, 255)", // 良好 #00ccff
    Ⅲ类: "rgb(0, 255, 0)", // 轻度污染 #00ff00
    Ⅳ类: "rgb(255, 255, 0)", // 中度污染 #ffff00
    Ⅴ类: "rgb(255, 155, 0)", // 重度污染 #ff9b00
    劣Ⅴ类: "rgb(255, 0, 0)", // 严重污染 #ff0000
    无数据: "rgb(128, 128, 128)", 
    1: "rgb(204, 255, 255)",
    2: "rgb(0, 204, 255)",
    3: "rgb(0, 255, 0)",
    4: "rgb(255, 255, 0)",
    5: "rgb(255, 155, 0)",
    6: "rgb(255, 0, 0)",
  };

  return colorMap[qualityLevel] || "rgba(128, 128, 128, 0.8)"; // 默认灰色
}

// 自动监测站
const zdWaterQualityImages = {
  国控: {
    1: require("@/assets/map/water/国控1.png"),
    2: require("@/assets/map/water/国控2.png"),
    3: require("@/assets/map/water/国控3.png"),
    4: require("@/assets/map/water/国控4.png"),
    5: require("@/assets/map/water/国控5.png"),
    6: require("@/assets/map/water/国控6.png"),
  },
  省控: {
    1: require("@/assets/map/water/省控1.png"),
    2: require("@/assets/map/water/省控2.png"),
    3: require("@/assets/map/water/省控3.png"),
    4: require("@/assets/map/water/省控4.png"),
    5: require("@/assets/map/water/省控5.png"),
    6: require("@/assets/map/water/省控6.png"),
  },
  市控: {
    1: require("@/assets/map/water/市控1.png"),
    2: require("@/assets/map/water/市控2.png"),
    3: require("@/assets/map/water/市控3.png"),
    4: require("@/assets/map/water/市控4.png"),
    5: require("@/assets/map/water/市控5.png"),
    6: require("@/assets/map/water/市控6.png"),
  },
  县控: {
    1: require("@/assets/map/water/县控1.png"),
    2: require("@/assets/map/water/县控2.png"),
    3: require("@/assets/map/water/县控3.png"),
    4: require("@/assets/map/water/县控4.png"),
    5: require("@/assets/map/water/县控5.png"),
    6: require("@/assets/map/water/县控6.png"),
  },
};

// 手工监测站
const sgWaterQualityImages = {
  1: require("@/assets/map/water/一类水质.png"),
  2: require("@/assets/map/water/二类水质.png"),
  3: require("@/assets/map/water/三类水质.png"),
  4: require("@/assets/map/water/四类水质.png"),
  5: require("@/assets/map/water/五类水质.png"),
  6: require("@/assets/map/water/劣五类水质.png"),
};

// 水源地水质监测站
const shuiyuandiStationImages = {
  1: require("@/assets/map/water/水源站1.png"),
  2: require("@/assets/map/water/水源站2.png"),
  3: require("@/assets/map/water/水源站3.png"),
  4: require("@/assets/map/water/水源站4.png"),
  5: require("@/assets/map/water/水源站5.png"),
  6: require("@/assets/map/water/水源站6.png"),
};

const qushuikouImage = require("@/assets/map/water/地表水取水口.png");
const wuranyuanImage = require("@/assets/map/pollution/污染源.png");
// const paiwukouImage = require("@/assets/map/paiwukou/入河排污口.png");
const paiwukouImages = {
  1: require("@/assets/map/paiwukou/城镇污水处理厂排污口.png"),
  2: require("@/assets/map/paiwukou/工业排污口.png"),
  3: require("@/assets/map/paiwukou/农业排口.png"),
  4: require("@/assets/map/paiwukou/其他排污口.png"),
};

const yujingImage = require("@/assets/map/warn/warn.png");
/**
 * 创建自动监测站样式（使用图片）
 * @param {ol.Feature} feature - 要素对象
 * @returns {Style} 样式对象
 */
export function createWaterSectionStyle(feature) {
  const properties = feature.getProperties();
  const controllevel = properties.controllevel || "省控";
  const qualityLevel = properties.qualityLevel || "2";
  const stationName = properties.name || properties.stationName || "";

  // 获取对应的图片
  const imageSrc =
    zdWaterQualityImages[controllevel][qualityLevel] ||
    zdWaterQualityImages["省控"]["2"]; // 默认使用二类水质图片

  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
    text: new Text({
      text: stationName,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      // stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}
/**
 * 创建实验室数据样式（使用图片）
 * @param {ol.Feature} feature - 要素对象
 * @returns {Style} 样式对象
 */
export function createShougongduanStyle(feature) {
  const properties = feature.getProperties();
  const name = properties.name || properties.stationName || "";
  const qualityLevel = properties.qualityLevel || "2";
  // 获取对应的图片
  const imageSrc =
    sgWaterQualityImages[qualityLevel] || sgWaterQualityImages["2"]; // 默认使用二类水质图片

  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
    text: new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}
/**
 * 创建水源地水质监测站样式
 * @param {ol.Feature} feature - 要素对象
 * @returns {Style} 样式对象
 */
export function createshuiyuandiStationStyle(feature) {
  const properties = feature.getProperties();
  const name = properties.name ||"";
  const qualityLevel = properties.qualityLevel || "2";
  // 获取对应的图片
  const imageSrc =
    shuiyuandiStationImages[qualityLevel] || shuiyuandiStationImages["2"]; // 默认使用二类水质图片

  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
        text: new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}
export function createQushuikouStyle(feature) {
    const properties = feature.getProperties();

  const name = properties.name || properties.outfallname || "";
  // 获取对应的图片
  const imageSrc = qushuikouImage; // 默认使用二类水质图片
  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
      text: new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}
export function createWuranyuanStyle2(feature) {
  // 获取对应的图片
  const imageSrc = wuranyuanImage; // 默认使用二类水质图片
   const properties = feature.getProperties();
    const name = properties.name || properties.outfallname || "";
  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
            text: new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}

 function createWuranyuanStyle(feature, resolution) {
    const showTextAtResolution =  0.00034332275390625; // 设置阈值分辨率
    console.log("createWuranyuanStyle resolution:", resolution);
  // 获取对应的图片
  const imageSrc = wuranyuanImage; // 默认使用二类水质图片
   const properties = feature.getProperties();
    const name = properties.name || properties.outfallname || "";
  const baseStyle= new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
            text: new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });


    if (resolution <= showTextAtResolution) {
      baseStyle.setText(
new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }))
    } else {
      baseStyle.setText(null);
    }

    return baseStyle;
  }
export function createPaiwukouStyle(feature) {
  const properties = feature.getProperties();
  const outfallcode = properties.outfallcode || "4";
  // 获取对应的图片
  const imageSrc = paiwukouImages[outfallcode] || paiwukouImages["4"]; // 默认使用其他排污口
  const name = properties.name || properties.outfallname || "";
  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
        text: new Text({
      text: name,
      offsetY: 25,
      font: "14px sans-serif",
      fill: new Fill({ color: "#333" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}

export function createYujingStyle(feature) {
  // 获取对应的图片
  const imageSrc = yujingImage; // 默认使用二类水质图片
  return new Style({
    image: new Icon({
      src: imageSrc,
      scale: 0.75,
      anchor: [0.5, 0.5],
    }),
  });
}
/**
 * 根据创建点样式
 * @param {string} layerId - 图层id
 * @returns {Function} 样式函数
 */
export function createPointStyleFunction(layerId) {
  //console.log("layerId", layerId);
  // 水质监测图层样式
  if (
    layerId === "guokongzhan" ||
    layerId === "shengkongzhan" ||
    layerId === "shikongzhan" ||
    layerId === "xiankongzhan"
  ) {
    return createWaterSectionStyle;
  }

    if (
    layerId === "zidongzhan"
  ) {
    return createWaterSectionStyle;
  }
  // 水质监测图层样式
  if (layerId === "shougongduanmian") {
    return createShougongduanStyle;
  }

  if (layerId === "shuiyuandiStation") {
    return createshuiyuandiStationStyle;
  }
  if (layerId === "qushuikou") {
    return createQushuikouStyle;
  }
  if (layerId === "wuranyuan") {
    return createWuranyuanStyle;
  }
  if (layerId === "ruhepaiwukou") {
    return createPaiwukouStyle;
  }
  if (layerId === "yujing") {
    return createYujingStyle;
  }

  // 默认点样式
  return new Style({
    image: new Circle({
      radius: 6,
      fill: new Fill({ color: "rgba(255, 0, 0, 0.6)" }),
      stroke: new Stroke({ color: "#fff", width: 2 }),
    }),
  });
}

export function createRegionStyleFunction() {
    return new Style({
      stroke: new Stroke({
        color: 'rgba(0, 0, 0, 0.8)',  // 黑色边框
        width: 2
      }),
      fill: new Fill({
        color: 'rgba(0, 0, 0, 0)'  // 透明填充
      })
    });

}
export function createRiverStyle(options = {}) {
  const {
    color = 'rgba(64, 164, 223, 0.8)', // 蓝色调，模拟真实河流颜色
    width = 3
  } = options;

  return new Style({
    stroke: new Stroke({
      color: color,
      width: width
    })
  });
}

export function createGeojsonStyleFunction(layerId) {
   if (layerId === "base_region") {
    return createRegionStyleFunction;
  }
  if (layerId === "base_river") {
    return createRiverStyle;
  }

}
export default {
  createPointStyleFunction,
  createGeojsonStyleFunction

};
